//
//  AHFSingerViewController.m
//  SlowPlay
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AHFSingerViewController.h"
#import "SIngerRecmmendListModel.h"
#import "RGCardViewLayout.h"
#import "RGreadCell.h"

#import "AHFSingerListViewController.h"

@interface AHFSingerViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *recommendListArray; /**< 歌手推荐列表*/

@property (nonatomic, strong) UICollectionView *cardCollectionVIew; /**< 卡片*/

@end


static NSString *strCollectionIndenifer = @"strCollectionIndenifer";
@implementation AHFSingerViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.recommendListArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getRecommendListArrayData];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"drawerbackpic.jpg"]];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.backgroundColor = [UIColor redColor];
    backButton.frame = CGRectMake(30, 40, 70,35);
    [backButton setTitle:@"back" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backbUttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    [self.view addSubview:self.cardCollectionVIew];
    self.cardCollectionVIew.delegate = self;
    self.cardCollectionVIew.dataSource = self;
    
    
}

#pragma mark - 返回按钮
- (void)backbUttonAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - 获取数据
- (void)getRecommendListArrayData
{
    
    [NetWork netWorkGETRequestWithURLAndNoMub:SingerRecommendListNet Paramater:nil ResultBlock:^(id result) {
        NSDictionary *listDic = (NSDictionary *)result;
        NSArray *dataArray = [listDic objectForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            SIngerRecmmendListModel *model = [[SIngerRecmmendListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.recommendListArray addObject:model];
            
            NSLog(@"%@", model.title);
            
            if (self.recommendListArray.count > 0) {
                [_cardCollectionVIew reloadData];
            }
        }
    }];
}


#pragma mark - 创建卡片式

//懒加载方式创建
- (UICollectionView *)cardCollectionVIew
{
    if (_cardCollectionVIew != nil) {
        return _cardCollectionVIew;
    }
    RGCardViewLayout *layout = [[RGCardViewLayout alloc] init];
    self.cardCollectionVIew = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    self.cardCollectionVIew.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"drawerbackpic.jpg"]];
    self.cardCollectionVIew.pagingEnabled = YES;
    [self.cardCollectionVIew registerClass:[RGreadCell class] forCellWithReuseIdentifier:strCollectionIndenifer];
    return _cardCollectionVIew;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.recommendListArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SIngerRecmmendListModel *model = [self.recommendListArray objectAtIndex:indexPath.section];
    RGreadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strCollectionIndenifer forIndexPath:indexPath];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"drawerbackpic.jpg"]];
    
    return cell;
}

//点击跳转下一界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.section);
    SIngerRecmmendListModel *model = [self.recommendListArray objectAtIndex:indexPath.section];
    AHFSingerListViewController *listVC = [[AHFSingerListViewController alloc] init];
    listVC.id = model.id;
    UINavigationController *listNavi = [[UINavigationController alloc] initWithRootViewController:listVC];
    listNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    [self presentViewController:listNavi animated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
